<template>
    <div class="searchTop">
        <div class="searchTopNav">
            <div class="back" @click="$router.go(-1)">
                <svg class="icon" aria-hidden="true">
                    <use xlink:href="#icon-zuojiantou"></use>
                </svg>
            </div>
            <div class="right">
                <input type="text" v-model="searchKeyword" :placeholder="placeholder" @keydown.enter="saveKeyWord">
            </div>
        </div>
        <div class="history" v-show="searchSongs.length==0">
            <div class="historyLeft">历史</div>
            <div class="historyRight">
                <div @click="historySearch(item)" class="keywordItem" v-for="(item,i) in KeywordList" :key="i">{{item}}</div>
            </div>
        </div>
        <div class="playlist" v-show="searchSongs.length!==0">
        <div class="playlist-top">
            <div class="left">
                <svg class="icon" aria-hidden="true">
                    <use xlink:href="#icon-bofang2"></use>
                </svg>
                <div class="text">
                    <div class="title">播放全部</div>
                    <div class="num">(共{{searchSongs.length}}首)</div>
                </div>
            </div>
        </div>
        <!-- 歌单 -->
        <div class="list">
            <div class="playItem" v-for="(item,index) in searchSongs" :key="index"  @click="setPlayIndex(index)">
                <div class="left">
                    <div class="index">{{index+1}}</div>
                    <div class="content">
                        <div class="title">{{item.name}}</div>
                        <div class="anthor">
                            <span class="tag" v-for="(tag,index) in searchSongs.tags" :key="index">
                                {{tag}}
                            </span>
                            <!-- /search?keywords= 海阔天空 -->
                            <span>{{item.album.name}}</span>
                            <!-- /cloudsearch?keywords= 海阔天空  -->
                            <!-- <span>{{item.al.name}}</span> -->
                        </div>
                    </div>
                </div>
                <div class="right">
                    <svg class="icon" aria-hidden="true" @click="setPlay(item,index)">
                        <use xlink:href="#icon-bofang1"></use>
                    </svg>
                    <svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-icmore"></use>
                    </svg>
                </div>
            </div>
        </div>
    </div>
    </div>
</template>

<script>
    import {searchMusic} from '@/api/index'
    export default {
        data:function(){
            return{
                placeholder:"陈奕迅",
                // 搜索的历史记录
                KeywordList:[],
                // 文本框绑定
                searchKeyword:'',
                // 搜索之后的记录存储
                searchSongs:[]
            }
        },
        beforeMount(){
            this.KeywordList = localStorage.KeywordList?JSON.parse(localStorage.KeywordList):[]
        },
        methods:{
            saveKeyWord:async function(){
                this.KeywordList.push(this.searchKeyword)
                this.KeywordList = Array.from(new Set(this.KeywordList))
                // 显示搜索记录10条
                if(this.KeywordList.length>10){
                   this.KeywordList = this.KeywordList.slice(this.KeywordList.length-10,this.KeywordList.length)
                }
                // 添加存储记录
                localStorage.KeywordList = JSON.stringify(this.KeywordList)
                // 获取搜索歌单
                let result = await searchMusic(this.searchKeyword)
                // 赋值给this.searchSongs
                this.searchSongs = result.data.result.songs
                // console.log(result)
            },
            historySearch(keyword){
                // 文本框绑定的数据,点击历史记录把历史记录添加到搜索框中
                this.searchKeyword = keyword
                // 再次调用
                this.saveKeyWord()
            },
            setPlay(item,i){
                item.al = item.album
                item.al.picUrl = item.album.artist.img1v1Url
                this.$store.commit('pushPlayList',item)
                this.$store.commit('setPlayIndex',this.$store.state.playlist.length-1)
            }
        }
    }
</script>

<style lang="less" scoped>
    .searchTop{
        width:7.5rem;
        height: calc(100vh - 1.2rem);
        padding: 0 0.4rem;
        .searchTopNav{
            display: flex;
            width: 100%;
            height: 1.2rem;
            align-items: center;
            overflow: scroll;
            position: fixed;
            top: 0;
            left:0;
            .icon{
                width: 0.5rem;
                height: 0.5rem;
                margin-left: 20px;
            }
            .right{
                padding: 0 0 0 0.4rem;
                flex: 1;
                input{
                    border: none;
                    border-bottom: 1px solid #666;
                    outline: none;
                    width: 90%;
                    height: 0.5rem;
                }
            }
        }
        .history{
            display: flex;
            margin-top: 1.2rem;
            .historyLeft{
                width: 1.2rem;
                height: 0.6rem;
                font-weight: 900;
                margin: 0.2rem 0rem;
                line-height: 0.6rem;
            }
            .historyRight{
                color: #666;
                display: flex;
                flex-wrap: wrap;
                flex: 1;
                .keywordItem{
                    background-color: #eee;
                    height: 0.6rem;
                    padding: 0 0.2rem;
                    border-radius: 0.4rem;
                    line-height: 0.8rem;
                    text-align: center;
                    margin: 0.2rem 0.1rem;
                }
            }
        }
        .playlist{
            width: 7rem;
            background-color: #fff;
            border-top-left-radius: 0.3rem;
            border-top-right-radius: 0.3rem;
            margin-top: 0.4rem;
            position: fixed;
            top: 1.2rem;
            bottom: 1.2rem;
            overflow: scroll;
        .playlist-top{
            display: flex;
            justify-content: space-between;
            height: 0.8rem;
            align-items: center;
            .left{
                display: flex;
                align-items: center;
                .icon{
                    width: 0.4rem;
                    height:0.4rem;
                }
                .title{
                    font-size: 0.35rem;
                    font-weight: 900;
                }
                .num{
                    font-size: 0.28rem;
                    color: #666;
                }
                .text{
                    display: flex;
                    align-items: center;
                    margin-left: 0.25rem;
                }
            }
            .btn{
                  font-size: 0.28rem;
                  color: #fff;
                  background-color: orangered; 
                  height: 0.6rem;
                  padding: 0.1rem;
                  border-radius: 0.4rem;
            }
        }
        .list{
            margin-bottom: 60px;
            .playItem{
                display: flex;
                justify-content: space-between;
                height: 1.5rem;
                .left{
                    display: flex;
                    align-items: center;
                    color: #666;
                    .index{
                        width: 0.3rem;
                    }
                    .content{
                        margin-left: 0.4rem;
                    }
                    .title{
                        font-size: 0.20rem;
                        font-weight: 900;
                        color: #000;
                        margin-bottom: 0.1rem;
                    }
                    .tag{
                        font-size: 0.2rem;
                        color:orangered;
                        border: 1px solid orangered;
                        border-radius: 0.1rem;
                        color:#666;
                    }
                    .anthor{
                        color: #666;
                        span{
                            font-size: 0.1rem;
                        }
                    }
                }
                .right{
                    display: flex;
                    align-items: center;
                    .icon{
                        margin: 0 0.15rem;
                    }
                }
                
            }
        }
    }
    }
</style>
